package cn.z2huo.core.util.object;

import cn.hutool.core.bean.BeanUtil;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

/**
 * <p>
 *
 * @author z2huo
 */
public final class BeanUtils {

    public static <T> T toBean(Object source, Class<T> targetClass) {
        return BeanUtil.toBean(source, targetClass);
    }

    public static <T> T toBean(Object source, Class<T> targetClass, Consumer<T> peek) {
        T target = toBean(source, targetClass);
        if (target != null) {
            peek.accept(target);
        }
        return target;
    }

    public static <S, T> List<T> copyToList(List<S> source, Class<T> targetType) {
        if (source == null) {
            return null;
        }
        return source.stream().map(element -> toBean(element, targetType)).filter(Objects::nonNull).toList();
    }

    public static <S, T> List<T> copyToList(List<S> source, Class<T> targetType, Consumer<T> peek) {
        List<T> list = copyToList(source, targetType);
        if (list != null) {
            list.forEach(peek);
        }
        return list;
    }

    public static void copyProperties(Object source, Object target) {
        if (source == null || target == null) {
            return;
        }
        BeanUtil.copyProperties(source, target, false);
    }

    private BeanUtils() {
    }
}
